<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\NewsCategory;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;

class NewsCategoryController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('资讯');
            $content->description('分类');

            $content->row(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->append(NewsCategory::tree(function (Tree $tree){
                        $tree->disableCreate();
                        $tree->branch(function ($branch) {
                            $src = config('admin.upload.host') . '/' . $branch['logo'] ;
                            $logo = "<img src='$src' style='max-width:15px;max-height:15px; margin-right: 10px;' class='img'/>";
                            return "$logo{$branch['title']}";
                        });
                    }));
                });
                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_url('news-category'));
                    $form->select('parent_id', '上级目录')->options(NewsCategory::selectOptions())->setWidth(4);
                    $form->text('title','分类标题')->setWidth(4);

                    $form->switch('can_post','前台可投稿');
                    $form->switch('need_audit','需要审核');
                    $form->text('order','排序')->setWidth(2);
                    $form->switch('status','状态');

                    $form->image('logo','分类LOGO');

                    $column->append((new Box(trans('admin::lang.new'), $form))->style('success'));
                });
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(NewsCategory::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(NewsCategory::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('parent_id', '上级目录')->options(NewsCategory::selectOptions())->setWidth(4);
            $form->text('title','分类标题')->setWidth(4);

            $form->switch('can_post','前台可投稿');
            $form->switch('need_audit','需要审核');
            $form->text('order','排序')->setWidth(2);
            $form->switch('status','状态');

            $form->image('logo','分类LOGO');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
